#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
using namespace std;
typedef pair<int, int> pii;
const int N = 1010;
vector<pii> as[N];
int f[N];
int n, m;
int main()
{
    cin >> m >> n;
    int cnt = 0;
    for (int i = 1; i <= n; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        cnt = max(cnt, c);
        as[c].push_back({ a,b });
    }
    for (int i = 1; i <= cnt; i++)
    {
        for (int j = m; j >= 0; j--)
        {
            for (auto& e : as[i])
            {
                int a = e.first;
                int b = e.second;
                if (j >= a)
                {
                    f[j] = max(f[j], f[j - a] + b);
                }
            }
        }
    }
    cout << f[m];
    return 0;
}